package com.fiberhome.boot.conf;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.druid.pool.DruidDataSource;
import com.fiberhome.boot.conf.datasource.MysqlDataSourceProperties;
import com.fiberhome.boot.conf.datasource.OracleDataSourceProperties;
import com.fiberhome.boot.conf.datasource.SqlServerDataSourceProperties;
import com.fiberhome.boot.conf.interceptor.MyInterceptor;



@Configuration
@EnableConfigurationProperties({MysqlDataSourceProperties.class,OracleDataSourceProperties.class,SqlServerDataSourceProperties.class})
@ConditionalOnMissingClass
public class AppConfig extends WebMvcConfigurerAdapter {

	@Autowired
	MysqlDataSourceProperties mysqlDataSourceProperties;
	
	@Autowired
	OracleDataSourceProperties OracleDataSourceProperties;
	
	@Autowired
	SqlServerDataSourceProperties sqlserverDataSourceProperties;
	
    //主数据源mysql
    @Bean(name = "mysqlDataSource")
    @Qualifier("mysqlDataSource")
    @Primary
    public DataSource mysqlDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(mysqlDataSourceProperties.getDriverClassName());
        dataSource.setUrl(mysqlDataSourceProperties.getUrl());
        dataSource.setUsername(mysqlDataSourceProperties.getUsername());
        dataSource.setPassword(mysqlDataSourceProperties.getPassword());
        
        try {
            dataSource.init();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataSource;
    }
    
    //从数据源oracle
    @Bean(name = "oracleDataSource")
    @Qualifier("oracleDataSource")
    public DataSource oracleDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(OracleDataSourceProperties.getDriverClassName());
        dataSource.setUrl(OracleDataSourceProperties.getUrl());
        dataSource.setUsername(OracleDataSourceProperties.getUsername());
        dataSource.setPassword(OracleDataSourceProperties.getPassword());
        
        try {
            dataSource.init();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataSource;
    }
    
    
    //从数据源sqlserver
    @Bean(name = "sqlserverDataSource")
    @Qualifier("sqlserverDataSource")
    public DataSource sqlserverDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(sqlserverDataSourceProperties.getDriverClassName());
        dataSource.setUrl(sqlserverDataSourceProperties.getUrl());
        dataSource.setUsername(sqlserverDataSourceProperties.getUsername());
        dataSource.setPassword(sqlserverDataSourceProperties.getPassword());
        
        try {
            dataSource.init();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataSource;
    }
    
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器组成一个拦截器链
        // addPathPatterns 用于添加拦截规则
        // excludePathPatterns 用户排除拦截
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }
	
}
